package com.hspedu.homework;

import java.util.Random;
import java.util.Scanner;

/**
 * @author DL5O
 * @version 1.0
 */
public class Homework01 {
    public static void main(String[] args) {
        A a = new A();
        B b = new B(a);
        a.setName("A");
        b.setName("B");
//        a.setDaemon(true);
        a.start();
        b.start();

    }
}

class A extends Thread {
    private int num;
    static boolean loop = true;
    private Random random;

    public A() {
        random = new Random();
    }

    public void showNum() {
        //nextInt(int bound)
        //返回伪随机的，均匀分布 int值介于0（含）和指定值（不包括），从该随机数生成器的序列绘制。
        num = random.nextInt(101);//生成0~100的随机数
        //num = (int)Math.random()*(100-0+1)+0;
        System.out.println("随机数：" + num);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    @Override
    public void run() {
        while (loop){
            showNum();
        }
    }
}

//用来控制A线程退出的线程B
class B extends Thread {
    private String str;
    private A a;
    Scanner scanner;

    public B(A a) {
        this.a = a;
        scanner = new Scanner(System.in);
    }

    @Override
    public void run() {
        synchronized (B.class) {
            while (true) {
                str = scanner.next();
                if (str.equals("Q")) {
                    a.loop = false;
                    break;
                }
            }
        }
    }
}